package com.zrx.springboot.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrx.springboot.entity.Address;
import com.zrx.springboot.entity.Book;
import com.zrx.springboot.entity.User;
import com.zrx.springboot.service.AddressService;
import com.zrx.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class BackUserController {

    @Autowired
    private UserService userService;
    @Autowired
    private AddressService addressService;

    @GetMapping("/user_table")
    public String mainPage(HttpSession session) {

        return "form/user_table";

    }
    /**
     * 获取用户信息
     */
    @RequestMapping("/getuserData")
    public String getUserData(Model model, Integer page, Integer size){
        System.out.println("显示图书信息------------");
        //mybatis puls分页
        IPage<User> iPage = userService.page(new Page<>(page,size));
        model.addAttribute("iPage", iPage);
        model.addAttribute("pre", iPage.getCurrent()-1);
        model.addAttribute("next", iPage.getCurrent()+1);
        model.addAttribute("cur", iPage.getCurrent());//当前页
        model.addAttribute("last", iPage.getPages());//总共页

        model.addAttribute("size",size);
        return "form/userDate";
    }
    /**
     * 根据名字模糊查询获取信息
     */
    @RequestMapping("/UserNameData")
    public String getBackUserNameData(Model model, Integer page,Integer size,String usernameid){
        System.out.println("显示用户信息------------");
        System.out.println(usernameid);
        //mybatis puls分页

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("username",usernameid);
        IPage<User> iPage = userService.page(new Page<>(page,size),queryWrapper);

        model.addAttribute("iPage", iPage);
        model.addAttribute("pre", iPage.getCurrent()-1);
        model.addAttribute("next", iPage.getCurrent()+1);
        model.addAttribute("cur", iPage.getCurrent());//当前页
        model.addAttribute("last", iPage.getPages());//总共页
        model.addAttribute("size",size);
        return "form/userDate";
    }

    /**
     *根据id查找
     */
    @RequestMapping("/getuserByID")
    public String getuserByID(Integer id,Model model){
        User user = userService.getById(id);
        model.addAttribute("user",user);
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        List<Address> address = addressService.list(queryWrapper);
        model.addAttribute("address",address);
        return "form/user_table_user";
    }
    /**
     *根据id修改
     */
    @RequestMapping("/upuser")
    public String upuser(Integer id,String password){
        System.out.println(id);
        System.out.println(password);

        userService.upword(id,password);


        return "form/user_table";
    }

    /**
     *根据id删除
     */
    @RequestMapping("/deleteuser")
    public String deleteuser(Integer id){
        System.out.println(id);
        userService.removeById(id);



        return "form/user_table";
    }
}
